package restaurant.iterators;

import java.util.ArrayList;
import java.util.NoSuchElementException;

import restaurant.MenuItem;

/**
 * An iterator to view the menu by course.
 * 
 * @author Odessa
 */

public class ByCourseIterator implements Iterator
{

	/** The course to view. */
	private String course;

	/** The iterator through the menu. */
	private java.util.Iterator<MenuItem> menu_iterator;

	/** The next item in the menu. */
	private MenuItem nextItem;



	/**
	 * Instantiates a new by course iterator.
	 * 
	 * @param menu the menu of items.
	 * @param course the course by which to view the menu.
	 */
	public ByCourseIterator(ArrayList<MenuItem> menu, String course) {
		this.course = course;
		menu_iterator = menu.iterator();
	}



	/**
	 * Determines if there is another item on the menu in the course specified.
	 * 
	 * @return {@code true} if there are more items on the menu in this course.
	 * @see restaurant.iterators.Iterator#hasNext()
	 */
	@Override
	public boolean hasNext()
	{
		boolean matchFound = false;

		while (menu_iterator.hasNext()) {
			MenuItem temp = (MenuItem) menu_iterator.next();

			if (temp.getCourse().equals(course)) {
				matchFound = true;
				nextItem = temp;
				break;
			}
		}

		if (matchFound == true) {
		}
		else
			nextItem = null;

		return matchFound;
	}



	/**
	 * Returns the next item on the menu in this course.
	 * 
	 * @return the next item on the menu in this course.
	 * @throws NoSuchElementException if the menu has no more items in the
	 *             course.
	 * @see restaurant.iterators.Iterator#next()
	 */
	@Override
	public Object next() throws NoSuchElementException
	{
		if (nextItem == null) {
			throw new NoSuchElementException("There are no more " + course +
					" items on the menu.");
		}
		else
			return nextItem;
	}

}
